//该文件用于创建vuex中最为核心的 store
import { setTimeout } from 'core-js';
import Vue from 'vue' 
//引入vuex
import Vuex from 'vuex'

//求和相关的配置
const countOptions = {
    namespaced: true,
    actions: {
        jiaOdd(context, value){
            console.log('actions中的jiaOdd被调用了');
            if(context.state.sum % 2){
                context.commit('JIA', value)
            }
        },
        jiaWait(context, value){
            console.log('actions中的jiaWait被调用了');
            setTimeout(() => {
                context.commit('JIA', value)
            }, 500);
        },
    
    },
    mutations: {
        JIA(state, value){
            console.log('mutations中的JIA被调用了');
            state.sum += value
        },
        JIAN(state, value){
            console.log('mutations中的JIAN被调用了');
            state.sum -= value
        },
    },
    state: {
        sum: 0, //当前的和
        school: '西安文理',
        subject: '前端',
    },
    getters: {
        bigSum(state){
            return state.sum * 10
        }
    },
}

//人员相关的配置
import axios from 'axios'
const personOptions = {
    namespaced: true,
    actions: {
        addPersonWang(context, value){
            if(value.name.indexOf('王') === 0){
                context.commit('ADD_PERSON', value)
            }else{
                alert('添加的人必须姓王！')
            }
        },
        addPersonServer(context){
            axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(
                response => {
                    context.commit('ADD_PERSON', {id:nanoid(), name:response.data})
                },
                error => {
                    alert(error.message)
                }
            )
        }
    },
    mutations: {
        ADD_PERSON(state, value){
            console.log('mutations中的ADD_PERSON被调用了');
            state.personList.unshift(value)
        }
    },
    state: {
        personList: [
            {id: '001', name: '张三'}
        ]
    },
    getters: {
        firstPersonName(state){
            return state.personList[0].name
        }
    },
}


//应用vuex插件
Vue.use(Vuex)

//创建store并导出store
export default new Vuex.Store({
    modules: {
        countAbout: countOptions,
        personAbout: personOptions
    }
})
